package com.xquant.trade.risk.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageInfo;
import com.xquant.common.core.constant.DictConstant;
import com.xquant.trade.risk.mapper.GroupMapper;
import com.xquant.trade.risk.entity.VGroup;
import com.xquant.trade.trade.entity.Trade;
import com.xquant.trade.trade.mapper.TradeMapper;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * newCloud  组合管理
 */
@Component
@Transactional
public class GroupService {
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private TradeMapper tradeMapper;
    @Autowired
    private RiskService riskService;
    /**
     * 查询 组合列表
     */
    public PageInfo getVGroupList(Map<String, Object> param) {
        List<VGroup> list = groupMapper.getVGroupList(param);
        return new PageInfo<VGroup>(list);
    }

    /**
     * 查询 组合列表
     */
    public List queryVGroupCombox(Map<String, Object> param) {
        List<Map> list = groupMapper.queryVGroupCombox(param);
        return list;
    }

    /**
     * 新增 组合
     */
    public String getGroupId(Map<String, Object> param) {
        return groupMapper.getGroupId(param);
    }

    public List<String> getGroupUserAndDeptPerm(Map<String, Object> param) {
        List<String> userPermUserId = groupMapper.getUserPermUserId(param);
        List<String> deptPermDeptId = groupMapper.getDeptPermDeptId(param);
        userPermUserId.addAll(deptPermDeptId);
        return userPermUserId;
    }


    /**
     * 新增 组合
     */
    public int addVGroup(Map<String, Object> param) {
        groupMapper.updateUserPermGroup(param);
        groupMapper.updateDeptPermGroup(param);
        return groupMapper.addVGroup(param);
    }

    public String getDeptAncestors(String deptId) {
        return groupMapper.getDeptAncestors(deptId);
    }

    /**
     * 修改 组合权限
     */
    public int updateGroupPerm(Map<String, Object> param) {
        groupMapper.removeUserPermGroup(param);
        groupMapper.removeDeptPermGroup(param);
        groupMapper.updateUserPermGroup(param);
        return groupMapper.updateDeptPermGroup(param);
    }


    /**
     * 修改 组合
     */
    public int updateVGroup(Map<String, Object> param) {
        return groupMapper.updateVGroup(param);
    }

    /**
     * 删除 组合
     */
    public int deleteVGroup(Map<String, Object> param) {
        groupMapper.removeUserPermGroup(param);
        groupMapper.removeDeptPermGroup(param);
        return groupMapper.deleteVGroup(param);
    }


    /**
     * 移入 组合
     */
    public int upGroupBalance(Map<String, Object> param) {
        return groupMapper.upGroupBalance(param);
    }

    /**
     * 移出 组合
     */
    public int downGroupBalance(Map<String, Object> param) {
        return groupMapper.downGroupBalance(param);
    }

    /**
     * 定时任务  移入移出 组合 交易
     */
    public void handleGroupBalance(Map<String, Object> param) {
        param.put("isAuth", "false");
        String dealDate = MapUtils.getString(param, "dealDate");
        if(StrUtil.isEmpty(dealDate)){
            dealDate=riskService.getBlcDate();
        }
         List<VGroup> list = groupMapper.getVGroupList(param);
        Map groupMap = new HashMap();
        for (int i = 0; i < list.size(); i++) {
            VGroup group = list.get(i);
            groupMap.put("groupId", group.getGroupId());
            Map m = JSON.parseObject(group.getGroupSchema());
            String optionTypes = MapUtils.getString(m, "optionTypes");
            if (org.apache.commons.lang.StringUtils.isNotEmpty(optionTypes)) {
                m.put("optionTypes", optionTypes.split(","));
            }
            String accIdCps = MapUtils.getString(m, "accIdCps");
            if (org.apache.commons.lang.StringUtils.isNotEmpty(accIdCps)) {
                m.put("accIdCps", accIdCps.split(","));
            }
            String accIds = MapUtils.getString(m, "accIds");
            if (org.apache.commons.lang.StringUtils.isNotEmpty(accIds)) {
                m.put("accIds", accIds.split(","));
            }
            String underlyerIds = MapUtils.getString(m, "underlyerIds");
            if (org.apache.commons.lang.StringUtils.isNotEmpty(underlyerIds)) {
                m.put("underlyerIds", underlyerIds.split("\\|"));
            }
            //20221031 botao.yu  组合增加按系统日期筛选  -by P013XEQ-1922 监控和情景分析优化
            String begDate1Flag = MapUtils.getString(m, "begDate1Flag");
            String begDate2Flag = MapUtils.getString(m, "begDate2Flag");
            String endDate1Flag = MapUtils.getString(m, "endDate1Flag");
            String endDate2Flag = MapUtils.getString(m, "endDate2Flag");
            if(DictConstant.YES_NO.YES.equals(begDate1Flag)){
                m.put("begDate1",dealDate);
            }
            if(DictConstant.YES_NO.YES.equals(begDate2Flag)){
                m.put("begDate2",dealDate);
            }
            if(DictConstant.YES_NO.YES.equals(endDate1Flag)){
                m.put("endDate1",dealDate);
            }
            if(DictConstant.YES_NO.YES.equals(endDate2Flag)){
                m.put("endDate2",dealDate);
            }
            m.put("isAuth", "false");
            List<Trade> tradeList = tradeMapper.queryGroupBalanceByPage(m);
            m.put("groupId",group.getGroupId());
            groupMapper.deleteGroupBalanceByGroupId(m);
            for (int j = 0; j < tradeList.size(); j++) {
                Trade trade = tradeList.get(j);
                groupMap.put("tradeId", trade.getTradeId());
                groupMap.put("isAuth", "false");
                groupMap.put("iCode", trade.getiCode());
                groupMap.put("aType", trade.getaType());
                groupMap.put("mType", trade.getmType());
                groupMap.put("accId", trade.getAccId());
                groupMapper.upGroupBalance(groupMap);
            }
        }
    }

    public boolean checkGroupName(VGroup group) {
        VGroup getByName = groupMapper.checkGroupName(group.getGroupName());
        if (getByName==null){
            return true;
        }else {
            if (getByName.getGroupCode().equals(group.getGroupCode())){
                return true;
            }else {
                return false;
            }
        }
    }
    public boolean checkGroupCode(VGroup group) {
        VGroup getByCode = groupMapper.checkGroupCode(group.getGroupCode());
        if (getByCode==null){
            return true;
        }else {
                return false;
        }
    }

}
